package com.aube;

import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;

import com.aube.push.PushPhoneModel;
import com.aube.push.PushSetting;
import com.aube.tinker.TinkerPatchManager;
import com.huyn.baseframework.net.OpenApi;
import com.huyn.baseframework.share.ShareUtil;
import com.huyn.baseframework.utils.AppUtil;
import com.huyn.baseframework.utils.Constant;
import com.huyn.baseframework.utils.SharedPrefUtil;
import com.huyn.baseframework.utils.StorageUtil;
import com.huyn.baseframework.utils.StringUtils;
import com.huyn.baseframework.utils.UserUtil;
import com.huyn.baseframework.utils.Utils;
import com.mcxiaoke.packer.helper.PackerNg;
import com.umeng.analytics.MobclickAgent;
import com.xiaomi.channel.commonutils.logger.LoggerInterface;
import com.xiaomi.mipush.sdk.Logger;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by huyaonan on 16/11/9.
 */
public class AubeApplicationLikeHelper {

    protected static final String TAG = "AubeApplicationLike";

    public void onCreate(Application application, String xiaomiId, String xiaomiKey) {
        StaticApplicationContext.init(application);

        StorageUtil.initDiskCacheDir(application);
        initAppArgs(application);

        UserUtil.restoreIntance(application);

        //发送策略
        //MobclickAgent.updateOnlineConfig(getApplicationContext());
        //禁止默认的页面统计方式，这样将不会再自动统计Activity
        MobclickAgent.openActivityDurationTrack(false);

        if(Constant.DEBUG) {
            String apiPath = SharedPrefUtil.getInstance(application).getString(SharedPrefUtil.API_PATH);
            if (StringUtils.isNotBlank(apiPath))
                OpenApi.setRouter(apiPath);
        }

        ShareUtil.initContext(application);

        if(PushPhoneModel.getInstance(application).isXiaomi()) {
            registMiPush(application, xiaomiId, xiaomiKey);
            disableJPush(application);
        } else {
            enableJPush(application);
        }
    }

    private void initAppArgs(Application application) {
        try {
            Constant.APP_MOBILETYPE = Build.MODEL;
            Constant.APP_DEVICEID = AppUtil.getDeviceId(application);

            PackageManager manager = application.getPackageManager();
            PackageInfo info = manager.getPackageInfo(application.getPackageName(), 0);
            Constant.APP_VERSION = info.versionName;

            Constant.APP_TRUE_VERSION = SharedPrefUtil.getInstance(application).getString(TinkerPatchManager.TRUE_VERSION, info.versionName);

            // 如果没有使用PackerNg打包添加渠道，默认返回的是""
            // com.mcxiaoke.packer.helper.PackerNg
            final String market = PackerNg.getMarket(application, "default");
            // 或者使用 PackerNg.getMarket(Context,defaultValue)
            // 之后就可以使用了，比如友盟可以这样设置
            MobclickAgent.startWithConfigure(new MobclickAgent.UMAnalyticsConfig(application, application.getString(R.string.umeng_key), market));

            Constant.APP_SOURCE = market;

            //会因为权限问题导致异常,放在最后处理,以防影响其它的值的获取
            Constant.APP_IMEI = AppUtil.getIMEI(application);// 获取IMEI号
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void registMiPush(Application application, String id, String key) {
        StaticApplicationContext.MI_PUSH_ID = id;
        StaticApplicationContext.MI_PUSH_KEY = key;
        if(PushSetting.isPushOn(application)) {
            PushSetting.registMiPush(application, id, key);
        }

        LoggerInterface newLogger = new LoggerInterface() {

            @Override
            public void setTag(String tag) {
                // ignore
            }

            @Override
            public void log(String content, Throwable t) {
                Utils.Log(TAG, content);
            }

            @Override
            public void log(String content) {
                Utils.Log(TAG, content);
            }
        };
        Logger.setLogger(application, newLogger);
    }

    public void enableJPush(Context context) {
        if(Constant.DEBUG)
            JPushInterface.setDebugMode(true);
        else
            JPushInterface.setDebugMode(false);

        JPushInterface.init(context); // Init JPush

        if(PushSetting.isPushOn(context)) {
            JPushInterface.resumePush(context);
        } else {
            JPushInterface.stopPush(context);
        }
    }

    public void disableJPush(Context context) {
        if(Constant.DEBUG)
            JPushInterface.setDebugMode(true);
        else
            JPushInterface.setDebugMode(false);

        JPushInterface.init(context); // Init JPush
        JPushInterface.stopPush(context); // no disable api provided by jpush
    }
}
